<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 3.9.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="https://fonts.loli.net/css?family=Lato:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">
<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"monocy.gitee.io","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"mac"},"back2top":{"enable":true,"sidebar":true,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":true,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":"valine","storage":true,"lazyload":true,"nav":null,"activeClass":"valine"},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":false,"top_n_per_article":5,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="前言在做项目的整体框架布局时曾用了 3 个版本，由最开始兼容性最好的 position 布局，到中间过渡调整用的 calc 布局，再到目前使用的 flex 布局。因为不考虑兼容性问题，因此用最新的写法来多趟趟水，此篇就是对所写的布局的一些心得。">
<meta name="keywords" content="CSS">
<meta property="og:type" content="article">
<meta property="og:title" content="项目布局与 flex">
<meta property="og:url" content="https://monocy.gitee.io/2019/06/10/项目布局与-flex/index.html">
<meta property="og:site_name" content="沐光">
<meta property="og:description" content="前言在做项目的整体框架布局时曾用了 3 个版本，由最开始兼容性最好的 position 布局，到中间过渡调整用的 calc 布局，再到目前使用的 flex 布局。因为不考虑兼容性问题，因此用最新的写法来多趟趟水，此篇就是对所写的布局的一些心得。">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2022-07-26T13:13:08.487Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="项目布局与 flex">
<meta name="twitter:description" content="前言在做项目的整体框架布局时曾用了 3 个版本，由最开始兼容性最好的 position 布局，到中间过渡调整用的 calc 布局，再到目前使用的 flex 布局。因为不考虑兼容性问题，因此用最新的写法来多趟趟水，此篇就是对所写的布局的一些心得。">

<link rel="canonical" href="https://monocy.gitee.io/2019/06/10/项目布局与-flex/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>项目布局与 flex | 沐光</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">沐光</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">记录在前端之路的点点滴滴</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">31</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">15</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">61</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="reading-progress-bar"></div>

  <a href="https://github.com/kazehaiya" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://monocy.gitee.io/2019/06/10/项目布局与-flex/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="蔡胤">
      <meta itemprop="description" content="Just do IT!">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="沐光">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          项目布局与 flex
        </h1>

        <div class="post-meta">
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2022-07-26 21:13:08" itemprop="dateModified" datetime="2022-07-26T21:13:08+08:00">2022-07-26</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/CSS/" itemprop="url" rel="index"><span itemprop="name">CSS</span></a>
                </span>
            </span>

          
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2019/06/10/项目布局与-flex/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2019/06/10/项目布局与-flex/" itemprop="commentCount"></span>
    </a>
  </span>
  
  
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>2.7k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>2 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h3 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h3><p>在做项目的整体框架布局时曾用了 3 个版本，由最开始兼容性最好的 position 布局，到中间过渡调整用的 calc 布局，再到目前使用的 flex 布局。因为不考虑兼容性问题，因此用最新的写法来多趟趟水，此篇就是对所写的布局的一些心得。</p>
<a id="more"></a>
<h3 id="整体布局"><a href="#整体布局" class="headerlink" title="整体布局"></a>整体布局</h3><p>项目的整体外框为品字形布局，顶部为 logo 、主题和用户信息（hover 展示 poper 详情）；底部左侧为对应主题的图片，以及其一、二级菜单；底部右侧为内容区。此外最右侧还有一个浮动的快捷栏。</p>
<p>内容区顶部有公告信息（可展示也可隐藏），底部则是自适应内容区。大致结构如下：</p>
<p><img data-src="/images/project-container.jpg" alt="项目结构" style="width: 600px"></p>
<p>下面来说说如何来规划布局以及遇到的问题的处理。</p>
<h3 id="历史版本及其问题"><a href="#历史版本及其问题" class="headerlink" title="历史版本及其问题"></a>历史版本及其问题</h3><h4 id="position-版本"><a href="#position-版本" class="headerlink" title="position 版本"></a>position 版本</h4><p>早期项目并不是很复杂时，使用的是绝对定位的方式。当时顶部无主题、右侧无公告，同时也没有一直悬挂在右侧的浮动框和动画，因此处理比较简单。不过使用 position 来做定位，又一点不是很好的地方是左侧和顶部需要用 <code>margin</code> 撑开（防止内容被挡住），同时超出部分的滚动条展示，以及一部分切换动画的效果呈现最终让我下定决心来更改整体的布局。</p>
<h4 id="calc-版本"><a href="#calc-版本" class="headerlink" title="calc 版本"></a>calc 版本</h4><p>中期因为还在开发项目，因此采取了一个过渡方案 calc，将各部分的宽度计算出来，从而做到页面的布局展现。这个版本倒是没什么问题，在 chrome 上运行的很稳定，由于不清楚 calc 计算会不会影响页面的渲染速度，因此最后还是改成了 flex 布局。</p>
<h3 id="flex-布局分析"><a href="#flex-布局分析" class="headerlink" title="flex 布局分析"></a>flex 布局分析</h3><h4 id="整体结构"><a href="#整体结构" class="headerlink" title="整体结构"></a>整体结构</h4><p>整体的结构可分为上下结构，而下半部分又拆分为左右结构，因此，在设置完顶层的 <code>html</code> 和 <code>body</code> 之后，给项目的最外层 container 元素设以下属性作为外层容器：</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-class">.container</span> &#123;</span><br><span class="line">  <span class="attribute">height</span>: <span class="number">100%</span>;</span><br><span class="line">  <span class="attribute">width</span>: <span class="number">100%</span>;</span><br><span class="line">  <span class="attribute">display</span>: flex;</span><br><span class="line">  <span class="attribute">flex-direction</span>: column;</span><br><span class="line">  <span class="attribute">position</span>: relative;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>然后顶部设置固定高度 60px，底部默认 flex-grow: 1 撑开即可。然而在内容撑满后，这样设置出现了<a href="#固定高度内容收缩问题">顶部固定高度部分被压缩了</a>。</p>
<h4 id="顶部结构"><a href="#顶部结构" class="headerlink" title="顶部结构"></a>顶部结构</h4><p>顶部可分为三个部分：左侧的 logo、中间的主题列表以及右侧的用户头像部分。顶部基本太多问题，因为用户头像部分有多个不同的行内元素，而这块产生了<a href="#行内元素对齐问题">行内元素无法对齐的问题</a>（于是采用了最为方便的 flex 写法）</p>
<h4 id="底部结构"><a href="#底部结构" class="headerlink" title="底部结构"></a>底部结构</h4><p>底部内容区域分为左右结构，左侧菜单展示（用 element-menu 很简单），右侧就是内容区了。这部分左侧收缩有做 transition 动画，对于页面图表渲染较多的情况会比较卡（动画去掉了），基本上没遇到什么麻烦；右侧有一个很头疼的问题，那就是页面顶部的公告，因为是可有可无的，而公告下面的内容区要一直保证能够占满内容区（如果内容超出则显示滚动条）。</p>
<p>有人会说：“（折中方案）这有什么难的，内容区 flex 布局，上侧公告区（flex-shrink: 0），下部分内容区默认添上 <code>flex-grow: 1</code>，然后溢出滚动就可以了～”。如果事情能这么简单就好了。需求却并不是上下结构，而是一个整体，因为滚动条区域包裹了顶部公告，同时还要兼容以前的页面，即：</p>
<div class="note default">
            <p>当 “呈现的内容高度” + “公告高度（得考虑无公告的情况）” &lt; “内容区域高度” 时无滚动条；反之有滚动条。此外还需要做好老代码的兼容处理。</p>
          </div>
<p>emmmmm，果然世界上没有产品所想不出来的主意啊，当然，在“折中方案”上线之后，慢慢的终于摸索出了<a href="#内容区的智能滚动条问题">最佳方案</a>。</p>
<h4 id="右侧悬浮框"><a href="#右侧悬浮框" class="headerlink" title="右侧悬浮框"></a>右侧悬浮框</h4><p>右侧悬浮框虽然并无太多内容，但是需要在 Vue 项目内支持拖拽，大致为：拖拽时颜色变浅，停止拖拽时恢复原样。由于 Vue 提供 drag 方法，因此比较容易想到的方式是在监听拖拽的起止动作，然后动态绑定 class 控制其 <code>Visibility</code> 显隐性，然而基本上每次开始拖拽后立马就结束拖拽了，最终定位的<a href="#诡异的拖拽事件">问题</a>就在 <code>Visibility</code> 上。</p>
<h3 id="遇到的问题"><a href="#遇到的问题" class="headerlink" title="遇到的问题"></a>遇到的问题</h3><h4 id="固定高度内容收缩问题"><a href="#固定高度内容收缩问题" class="headerlink" title="固定高度内容收缩问题"></a>固定高度内容收缩问题</h4><p>在 flex 布局的模式下，子项的默认值为：</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">flex</span>: 0 1 <span class="selector-tag">auto</span>;</span><br></pre></td></tr></table></figure>
<p>其分别表示 <code>flex-grow</code> 为 0，即不会参与多余部分的分配（不可扩张）；<code>flex-shrink</code> 为 1，即当整体空间无法装下所有的内容时，该部分会按照一定的规则来压缩；<code>flex-basic</code> 为 auto，即默认以内容区高度为基础高度。</p>
<p>那么问题就来了。因为 <code>flex-basic</code> 为 auto，内容区域高度是由内容来决定的，虽然我们通过设置 <code>height: 60px</code> 将高度固定撑开，但是对于 flex 来说，我的 basic 部分并没有这么高（内容没有达到 60px），同时我这部分又是可压缩的，因此我可以匀一点来尽量使得另一部分能够撑满溢出的内容。而结果可想而知（顶部被压缩了）</p>
<p>解决的思路有三种：</p>
<ol>
<li>设置 <code>flex-shrink: 0</code> 来禁止其被压缩</li>
<li>设置 <code>min-height: 60px</code> 从而保证最低的高度</li>
<li>将其包裹的内容区高度设置为 60px</li>
</ol>
<h4 id="行内元素对齐问题"><a href="#行内元素对齐问题" class="headerlink" title="行内元素对齐问题"></a>行内元素对齐问题</h4><p>这部分先前有写过<a href="https://kazehaiya.github.io/2019/04/18/%E6%B5%85%E8%B0%88%E8%A1%8C%E5%86%85%E5%85%83%E7%B4%A0/" target="_blank" rel="noopener">浅谈行内元素</a>，因为各部分默认对齐方式不同，此处仅写出两种最为快捷的方法：</p>
<ol>
<li>设置父级为 flex，然后设置 <code>align-items: center</code></li>
<li>修改各子元素的对齐方式为 <code>vertical-align: center</code></li>
</ol>
<h4 id="内容区的智能滚动条问题"><a href="#内容区的智能滚动条问题" class="headerlink" title="内容区的智能滚动条问题"></a>内容区的智能滚动条问题</h4><p>问题简单的抽象为一个有固定宽高的 flex 布局的父容器（column 排序），其包含两个子节点：</p>
<p>第一个节点可有可无，呈现时为定高的状态；</p>
<p>第二个节点当内容不是很高时，其会自动填充剩余部分（剩余部分 + 此节点内容 + 第一个节点内容 = 父容器高度）；当内容有足够高时，父级会出现滚动条来显示溢出的内容。</p>
<p>老版本此部分的实现是：</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-class">.content</span> &#123;</span><br><span class="line">  <span class="attribute">height</span>: <span class="number">100%</span>;</span><br><span class="line">  <span class="attribute">width</span>: <span class="number">100%</span>;</span><br><span class="line">  <span class="attribute">position</span>: relative;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>理想中最完美的升级方案是在原有的基础上添加一个 <code>flex-grow: 1</code> 然后删除 <code>height: 100%</code>，但是由于要不影响原有的代码（仅使用 flex-grow 不知道为什么，某个页面会出现一个只滚动 1px 距离的滚动条，方案 pass），因此做了下改变即添加一项 <code>min-height: calc(100% - 60px)</code> 然后滚动条就没有了。</p>
<p>前后试了一下，发现必须动态计算的 <code>min-height</code> 值与固定值之和为 100% 时才生效，个人感觉应该是因为刚好占满整个屏幕，因此不会触发多余的 flex 的动态计算，因此也就没有额外的滚动条了；当顶部公告消失时， <code>min-height</code> &lt; <code>height</code> 值，使内容能自动充满屏幕。</p>
<blockquote>
<p>如果并非需要兼容历史代码，建议直接 flex-grow: 1，删除 height: 100% 这样应该会好调整一些。</p>
</blockquote>
<h4 id="诡异的拖拽事件"><a href="#诡异的拖拽事件" class="headerlink" title="诡异的拖拽事件"></a>诡异的拖拽事件</h4><p>最开始写拖拽方法时添加的类为：</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-class">.onDrag</span> &#123;</span><br><span class="line">  <span class="attribute">visibility</span>: hidden;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在拖拽时隐藏元素，松开时显示元素（最开始实验阶段的 vue/cli3 时可以通过此方法实现），后来版本升级后，该方法失效了，会使得隐藏后会立即事情元素焦点，然后立即出发了 dragend 事件，此较好的解决方法目前是将 <code>visibility: hidden</code> 改成了 <code>opacity: 0</code>，但是更新后拖拽过程中设置元素 <code>visibility: hidden;</code> 会使得焦点丢失，这个原因目前不是很明确。</p>
<h4 id="实例代码"><a href="#实例代码" class="headerlink" title="实例代码"></a>实例代码</h4><blockquote>
<p><a href="https://codepen.io/kazehaiya/project/editor/XNbLkL" target="_blank" rel="noopener">参考例子</a></p>
</blockquote>
<h3 id="参考文章"><a href="#参考文章" class="headerlink" title="参考文章"></a>参考文章</h3><ul>
<li><a href="https://www.cnblogs.com/chris-oil/p/5430137.html" target="_blank" rel="noopener">flex 设置成 1 和 auto 有什么区别</a></li>
<li><a href="https://www.w3.org/html/ig/zh/css-flex-1/" target="_blank" rel="noopener">W3C flex</a></li>
</ul>

    </div>

    
    
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>蔡胤
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://monocy.gitee.io/2019/06/10/项目布局与-flex/" title="项目布局与 flex">https://monocy.gitee.io/2019/06/10/项目布局与-flex/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">
          
          <div class="post-tags">
              <a href="/tags/CSS/" rel="tag"><i class="fa fa-tag"></i> CSS</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2019/06/06/JavaScript-对象/" rel="prev" title="JavaScript 对象">
      <i class="fa fa-chevron-left"></i> JavaScript 对象
    </a></div>
      <div class="post-nav-item">
    <a href="/2019/06/18/层叠上下文/" rel="next" title="层叠上下文">
      层叠上下文 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#前言"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#整体布局"><span class="nav-number">2.</span> <span class="nav-text">整体布局</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#历史版本及其问题"><span class="nav-number">3.</span> <span class="nav-text">历史版本及其问题</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#position-版本"><span class="nav-number">3.1.</span> <span class="nav-text">position 版本</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#calc-版本"><span class="nav-number">3.2.</span> <span class="nav-text">calc 版本</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#flex-布局分析"><span class="nav-number">4.</span> <span class="nav-text">flex 布局分析</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#整体结构"><span class="nav-number">4.1.</span> <span class="nav-text">整体结构</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#顶部结构"><span class="nav-number">4.2.</span> <span class="nav-text">顶部结构</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#底部结构"><span class="nav-number">4.3.</span> <span class="nav-text">底部结构</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#右侧悬浮框"><span class="nav-number">4.4.</span> <span class="nav-text">右侧悬浮框</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#遇到的问题"><span class="nav-number">5.</span> <span class="nav-text">遇到的问题</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#固定高度内容收缩问题"><span class="nav-number">5.1.</span> <span class="nav-text">固定高度内容收缩问题</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#行内元素对齐问题"><span class="nav-number">5.2.</span> <span class="nav-text">行内元素对齐问题</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#内容区的智能滚动条问题"><span class="nav-number">5.3.</span> <span class="nav-text">内容区的智能滚动条问题</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#诡异的拖拽事件"><span class="nav-number">5.4.</span> <span class="nav-text">诡异的拖拽事件</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#实例代码"><span class="nav-number">5.5.</span> <span class="nav-text">实例代码</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#参考文章"><span class="nav-number">6.</span> <span class="nav-text">参考文章</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="蔡胤"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">蔡胤</p>
  <div class="site-description" itemprop="description">Just do IT!</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">61</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">15</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">31</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/kazehaiya" title="GitHub → https://github.com/kazehaiya" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:c763416559@163.com" title="Mail → mailto:c763416559@163.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="https://weibo.com/Nora_Monocy" title="Weibo → https://weibo.com/Nora_Monocy" rel="noopener" target="_blank"><i class="fab fa-weibo fa-fw"></i></a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="http://liujinkai.com/" title="http://liujinkai.com/" rel="noopener" target="_blank">凯凯刘</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://weekly.75team.com/" title="https://weekly.75team.com/" rel="noopener" target="_blank">奇舞周刊</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://75.team/post/list" title="https://75.team/post/list" rel="noopener" target="_blank">奇舞团博客</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://tech.meituan.com/" title="https://tech.meituan.com/" rel="noopener" target="_blank">美团技术团队</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://tech.youzan.com/" title="https://tech.youzan.com/" rel="noopener" target="_blank">有赞技术团队</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="https://kalacloud.com" title="https://kalacloud.com" rel="noopener" target="_blank">卡拉云</a>
        </li>
    </ul>
  </div>

      </div>
        <div class="back-to-top motion-element">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        
  <div class="beian"><a href="https://beian.miit.gov.cn" rel="noopener" target="_blank">京ICP备2020036147号-1 </a>
      <img src="/images/beian.png" style="display: inline-block;"><a href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=42011702000651" rel="noopener" target="_blank">鄂公网安备 42011702000651 号 </a>
  </div>

<div class="copyright">
  
  &copy; 2018 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-dragon"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">蔡胤</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">101k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span class="post-meta-item-text">站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">1:32</span>
</div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/gh/theme-next/theme-next-pjax@0/pjax.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/medium-zoom@1/dist/medium-zoom.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>
<script src="/js/utils.js"></script><script src="/js/motion.js"></script>
<script src="/js/schemes/pisces.js"></script>
<script src="/js/next-boot.js"></script>
  <script>
var pjax = new Pjax({
  selectors: [
    'head title',
    '#page-configurations',
    '.content-wrap',
    '.post-toc-wrap',
    '.languages',
    '#pjax'
  ],
  switches: {
    '.post-toc-wrap': Pjax.switches.innerHTML
  },
  analytics: false,
  cacheBust: false,
  scrollTo : !CONFIG.bookmark.enable
});

window.addEventListener('pjax:success', () => {
  document.querySelectorAll('script[data-pjax], script#page-configurations, #pjax script').forEach(element => {
    var code = element.text || element.textContent || element.innerHTML || '';
    var parent = element.parentNode;
    parent.removeChild(element);
    var script = document.createElement('script');
    if (element.id) {
      script.id = element.id;
    }
    if (element.className) {
      script.className = element.className;
    }
    if (element.type) {
      script.type = element.type;
    }
    if (element.src) {
      script.src = element.src;
      // Force synchronous loading of peripheral JS.
      script.async = false;
    }
    if (element.dataset.pjax !== undefined) {
      script.dataset.pjax = '';
    }
    if (code !== '') {
      script.appendChild(document.createTextNode(code));
    }
    parent.appendChild(script);
  });
  NexT.boot.refresh();
  // Define Motion Sequence & Bootstrap Motion.
  if (CONFIG.motion.enable) {
    NexT.motion.integrator
      .init()
      .add(NexT.motion.middleWares.subMenu)
      .add(NexT.motion.middleWares.postList)
      .bootstrap();
  }
  NexT.utils.updateSidebarPosition();
});
</script>




  




  <script src="/js/local-search.js"></script>












    <div id="pjax">
  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/valine@1/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : true,
      notify     : true,
      appId      : 'GkgJm97N3UFm2mRb1eFcP2dn-gzGzoHsz',
      appKey     : 'kJ8tYUUkX4ifhWha8rjnxWS8',
      placeholder: "请不要吝惜你的笔头，来说点什么吧！",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : false,
      lang       : 'zh-cn' || 'zh-cn',
      path       : location.pathname,
      recordIP   : true,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

    </div>
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/live2dw/assets/Epsilon2.1.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":true,"scale":0.5},"react":{"opacity":0.7},"log":false});</script></body>
</html>
